package com.kfm.commons;

import com.kfm.config.FileConfig;
import com.kfm.entity.FileVo;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    public static List<FileVo> getDir() {
        return readFiles(FileConfig.getLocalPath(), "dir");
    }

    public static List<FileVo> readFiles(String path, String type) {
        File file = new File(path);
        File[] files = file.listFiles(f -> {
            return "dir".equalsIgnoreCase(type) ? f.isDirectory() : f.isFile();
        });
        List<FileVo> fileVos = new ArrayList<>();
        for (File f : files) {
            fileVos.add(new FileVo(f));
        }
        return fileVos;
    }

    public static List<FileVo> getFiles(String currentPath) {
        return readFiles(FileConfig.getLocalPath() + File.separator + currentPath, "file");
    }

    private static FTPClient getFTP() throws IOException {
        FTPClient client = new FTPClient();
        // 创建ftp连接
        client.connect(FileConfig.getIp(), Integer.parseInt(FileConfig.getPort()));
        client.login(FileConfig.getUsername(), FileConfig.getPassword());
        client.setConnectTimeout(50000);
        // 设置中文编码集，防止中文乱码
        client.setControlEncoding("UTF-8");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            System.out.println("未连接到FTP，用户名或密码错误。");
            client.disconnect();
            return null;
        } else {
            System.out.println("FTP连接成功。");
            return client;
        }
    }




}
